package liangtWorkSpace.simple;

import java.util.LinkedList;

/**
 * @ClassName CQueue
 * @Description : java实现队列
 * @Author : LiangT
 * @Date : 2020/6/30 19:28
 */
public class CQueue<T> {
    class Node<T>{
        private T t;
        private Node next;
    }
    private Node<T> head;

    public CQueue() {
        head = null;
    }

    /**
     * 新增
     * @param value
     */
    public void appendTail(T value) {
        if (value == null){
            throw new RuntimeException("参数不能为空");
        }
        if (head == null) {
            head = new Node<T>();
            head.t = value;
            head.next = null;
        } else {
            Node<T> temp = head;
            head = new Node<>();
            head.t = value;
            head.next = temp;
        }
    }

    /**
     * 删除
     * @return
     */
    public T deleteHead() {
        T t = head.t;
        head = head.next;
        return t;
    }
    public boolean isEmpty(){
        if (head == null){
            return true;
        }else {
            return false;
        }
    }

    public static void main(String[] args) {
        CQueue cQueue = new CQueue();
//        System.out.println(cQueue.isEmpty());
//        cQueue.appendTail("java");
//        cQueue.appendTail("C++");
//        cQueue.appendTail("C");
//        cQueue.appendTail("Python");
//        System.out.println(cQueue.deleteHead());
//        System.out.println(cQueue.deleteHead());
//        System.out.println(cQueue.deleteHead());
//        System.out.println(cQueue.deleteHead());
//        System.out.println(cQueue.isEmpty());

    }
}

/**
 * 实现栈
 */
class CQUEUE{
    class Node{
        int value;
        private Node next;
    }
    private Node head;
    public CQUEUE() {
        head = null;
    }

    public void appendTail(int value) {
        if(head == null){
            head = new Node();
            head.value = value;
            head.next = null;
        }else{
            Node temp = head;
            head = new Node();
            head.value = value;
            head.next = temp;
        }
    }

    public int deleteHead() {
        if(isEmpty(head)){
            return -1;
        }
        int t = head.value;
        head = head.next;
        return t;
    }

    public boolean isEmpty(Node node){
        if(node == null){
            return false;
        }
        return true;
    }
}

/**
 * 使用LinkedList实现栈
 * @param <T>
 */
class ListStack<T>{
    private LinkedList<T> linkedList = new LinkedList<>();

    /**
     * 入栈
     * @param t
     */
    public void push(T t){
        linkedList.add(t);
    }

    /**
     * 出栈
     * @return
     */
    public T pop(){
        return linkedList.removeFirst();
    }

    /**
     * 获取栈顶元素
     * @return
     */
    public T peek(){
        T t = null;
        if (!linkedList.isEmpty()){
            t = linkedList.getFirst();
        }
        return t;
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty(){
        return linkedList.isEmpty();
    }
}
